package com.lucien.admin.controller.chat;

import com.lucien.common.result.GlobalResult;
import com.lucien.system.domain.ChatMessage;
import com.lucien.common.fornt.ChatMsgFront;
import com.lucien.system.service.ChatMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Lucien
 * @Date 2021/5/30 11:28
 * @Version 1.0
 * @Description: 消息记录接口
 */
@RestController
@RequestMapping("/chat")
@Api(tags = "消息记录接口")
public class ChatMessageController {

    @Autowired
    private ChatMessageService messageService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加消息记录")
    public GlobalResult insertMsg(int toId, String content, String msgType) {
        int result = messageService.insertMsg(toId, content, msgType);
        if (result == 1) {
            return GlobalResult.success("消息记录成功", result);
        } else {
            return GlobalResult.error("消息记录失败！！！", result);
        }
    }


    @GetMapping("/msglist/{toId}/{formId}")
    @ApiOperation(value = "获取用户聊天记录")
    public GlobalResult msgList(@PathVariable("toId") Integer toId, @PathVariable("formId") Integer formId) {
        List<ChatMessage> result = messageService.msgList(toId, formId);
        return GlobalResult.success("获取聊天记录成功", result);
    }

    @GetMapping("/gettalklist/{userId}")
    @ApiOperation(value = "获取聊天记录列表")
    public GlobalResult getMsgList(@PathVariable("userId") Integer userId) {
        List<ChatMsgFront> result = messageService.getMsgList(userId);
        return GlobalResult.success("获取聊天记录成功", result);
    }

    @GetMapping("/delmsglist/{friend_id}")
    @ApiOperation(value = "删除用户全部聊天记录")
    public GlobalResult delMsgList(@PathVariable("friend_id") Integer friend_id) {
        int result = messageService.delMsg(friend_id);
        return GlobalResult.success("删除聊天记录成功", result);
    }

    @GetMapping("/delmsgbyid")
    @ApiOperation(value = "删除聊天记录")
    public GlobalResult delMsgById(@RequestParam(name = "ids", required = true) Integer[] ids) {
        int result = messageService.delMsgById(ids);
        return GlobalResult.success("删除聊天记录成功", result);
    }

    @GetMapping("/msgeecode")
    @ApiOperation(value = "撤回一条聊天记录")
    public GlobalResult msgRecode(@RequestParam(name = "id", required = true) Integer id) {
        int result = messageService.msgRecode(id);
        return GlobalResult.success("消息已撤回", result);
    }
}
